package dao.impl;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.sql.DataSource;

import util.DBUtil;

import org.springframework.stereotype.Repository;

import pojo.Customer;
import dao.CustomerDao;

@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao{
	@Resource(name="dataSource")
	private DataSource dataSource;
	public Customer addCustomer(Customer c) {
		Connection connection  = null;
		try {
			connection = dataSource.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		PreparedStatement ps = null;
		String sql = "insert into cust(id,cname,sal,sex,birth) values(?,?,?,?,?)";
		int n=0;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, c.getId());
			ps.setString(2, c.getCname());
			ps.setDouble(3, c.getSal());
			ps.setBoolean(4, c.isSex());
			ps.setDate(5, new Date(c.getBirth().getTime()));
			n = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.release(null, ps, connection);
		}
		return c;
	}

	public Customer updateCustomer(Customer c) {
		Connection connection  = null;
		try {
			connection =dataSource.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		PreparedStatement ps = null;
		String sql = "update cust set cname=?,sal=?,sex=?,birth=? where id=?";
		int n=0;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1,c.getCname());
			ps.setDouble(2, c.getSal());
			ps.setBoolean(3, c.isSex());
			ps.setDate(4, new Date(c.getBirth().getTime()));
			ps.setString(5, c.getId());
			n = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.release(null, ps, connection);
		}
		return c;
	}

	public List<Customer> findAllCustomers() {
		Connection connection  = null;
		try {
			connection = dataSource.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from cust";
		List<Customer> customers = new ArrayList<Customer>();
		Customer c = null;
		try {
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				c = new Customer();
				c.setId(rs.getString(1));
				c.setCname(rs.getString(2));
				c.setSal(rs.getDouble(3));
				c.setSex(rs.getBoolean(4));
				c.setBirth(rs.getDate(5));
				customers.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.release(null, ps, connection);
		}
		return customers;
	}

	public Customer deleteCustomer(String id) {
		Connection connection  = null;
		try {
			connection = dataSource.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		PreparedStatement ps = null;
		String sql = "delete from cust where id=?";
		Customer c=null;
		int n=0;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, id);
			n = ps.executeUpdate();
			c=new Customer(id);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.release(null, ps, connection);
		}
		return c;
	}
}
